﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Public
{
    public partial class wfVehicleList : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        string countryId = "";
        string cityId = "";
        string destionId = "";
        private void getArea2(string parentsId)
        {
            if (string.IsNullOrEmpty(parentsId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "全部";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                cityId = this.GetParamHtValue("cityId");
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                this.ExecJScript(@"updateSelet(""cityId_select""," + ret + @",""" + cityId + @""")");
                getArea3(cityId);
            }
        }
        private void getArea3(string parentsId)
        {
            if (string.IsNullOrEmpty(parentsId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                //string isAll = this.GetParamHtValue("isAll");
                //if (isAll.ToLower() == "true")
                //{
                //    DataRow dr = ds.Tables[0].NewRow();
                //    dr["COUNTRY_CITY_ID"] = "";
                //    dr["COUNTRY_CITY_NME"] = "全部";
                //    ds.Tables[0].Rows.InsertAt(dr, 0);
                //}
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "全部";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                destionId = this.GetParamHtValue("destionId");
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                this.ExecJScript(@"updateSelet(""destionId_select""," + ret + @",""" + destionId + @""")");

            }
        }
        private void initplace(string parentsId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                countryId = this.GetParamHtValue("countryId");
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "全部";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(countryId))
                {
                    countryId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                this.ExecJScript(@"updateSelet(""countryId_select""," + ret + @",""" + countryId + @""")");
                getArea2(countryId);

            }
        }

        private void GetCarType()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicle.GetVehicleType();
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 1000, "");
                this.ExecJScript(@"setVehicleType(" + retJson + ")");
            }
        }

        private void GetCarBrand()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicle.GetVehicleBrand();
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 1000, "");
                this.ExecJScript(@"setVehicleBrand(" + retJson + ")");
            }
        }

        private void GetVehicleList()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                int pageRows = 10;
                int.TryParse(this.GetParamHtValue("pageRows"), out pageRows);
                int pageIndex = 0;
                int.TryParse(this.GetParamHtValue("pIndex"), out pageIndex);
                int vehicleType = 0;
                int.TryParse(this.GetParamHtValue("vehicleType"), out vehicleType);
                int brand = 0;
                int.TryParse(this.GetParamHtValue("checkBrand"), out brand);
                decimal minVehiclePrice = 0;
                decimal.TryParse(this.GetParamHtValue("minVehiclePrice"), out minVehiclePrice);
                decimal maxVehiclePrice = 0;
                decimal.TryParse(this.GetParamHtValue("maxVehiclePrice"), out maxVehiclePrice);
                int minVehicleSeat = 0;
                int.TryParse(this.GetParamHtValue("minVehicleSeat"), out minVehicleSeat);
                int maxVehicleSeat = 0;
                int.TryParse(this.GetParamHtValue("maxVehicleSeat"), out maxVehicleSeat);
                int serviceType = 0;
                int.TryParse(this.GetParamHtValue("serviceType"), out serviceType);
                string kw = string.IsNullOrEmpty(this.GetParamHtValue("keyword")) ? string.Empty : "%" + this.GetParamHtValue("keyword") + "%";

                DataSet ds = vehicle.GetVehicleForPageview(pageRows, pageIndex, destionId, cityId, countryId, vehicleType, brand, minVehiclePrice, maxVehiclePrice, minVehicleSeat, maxVehicleSeat, serviceType, kw);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    string nodata = @"<tr class=""lastSearchContent""><td colspan=""4"" style=""color:#e23030; background-color:#e9ef5c;padding-left:20px;"">暂时没有您查找的数据...</td></tr>";
                    this.ExecJScript(@"$('#carList').html('" + nodata + "')");
                    return;
                }
                DataColumn dr = new DataColumn("VEHICLE_BRAND_NAME");
                ds.Tables[0].Columns.Add(dr);
                DataColumn dr1 = new DataColumn("VEHICLE_SERVICE_TYPE_NAME");
                ds.Tables[0].Columns.Add(dr1);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    ds.Tables[0].Rows[i]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_BRAND"].ToString()));

                    ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.ServiceTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE"].ToString()));
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 500, "");
                this.ExecJScript(@"
                            var _html = $('#svc_list_temp_vehicle').html();

                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#carList').html(render);
                            });jsPage('divPage', " + Convert.ToInt32(ds.Tables[0].Rows[0]["ROWSCOUNT"].ToString()) + ", " + pageRows + ", " + pageIndex + ", 'goPage');");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                //msBsc.AppLogic.DataAlc.CBizCustomerRegister dd = new AppLogic.DataAlc.CBizCustomerRegister();
                //dd.CryCustomerPwd();
                //this.Alert("5");
                string parentsId = this.GetParamHtValue("parentsId");
                initplace(parentsId);
                GetCarType();
                GetCarBrand();
                GetVehicleList();
                base.DoAction();
            }
            else if (this.strAction == "checkCountry")
            {
                string countryId = this.GetParamHtValue("countryId");
                using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
                {
                    DataSet ds = cbcc.SelectCountryByParentId(countryId);

                    string cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    this.ExecJScript(@"$('#cityId').val('" + cityId + "');refreshPage()");
                }
            }
        }
    }
}